package com.lzk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzk.common.BaseUtils;
import com.lzk.common.R;
import com.lzk.dto.DishDto;
import com.lzk.mapper.DishMapper;
import com.lzk.pojo.Category;
import com.lzk.pojo.Dish;
import com.lzk.pojo.DishFlavor;
import com.lzk.service.CategoryService;
import com.lzk.service.DIshFlavorService;
import com.lzk.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Resource
    private DishService dishService;
    @Resource
    private DIshFlavorService dishFlavorService;
    @Resource
    private CategoryService categoryService;
    @Override
    @Transactional
    public R<String> saveWithFlavor(DishDto dishDto) {
        dishService.save(dishDto);
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
        }
        dishFlavorService.saveBatch(flavors);
        return R.success("添加成功");
    }

    @Override
    public R<Page<DishDto>> myPage(int page, int pageSize, String name) {
        Page<Dish> dishPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.like(null!=name,Dish::getName,name);
        dishService.page(dishPage, lqw);
        Page<DishDto> dishDtoPage = new Page<>();
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        List<DishDto> dtoList = new ArrayList<>();
        List<Dish> records = dishPage.getRecords();
        for (Dish dish : records) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);
            Long categoryId = dish.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            dishDto.setCategoryName(categoryName);
            dtoList.add(dishDto);
        }
        dishDtoPage.setRecords(dtoList);
        return R.success(dishDtoPage);
    }

    @Override
    public R<DishDto> showDishWhenUpdate(Long id) {
        DishDto dishDto = new DishDto();
        Dish dish = dishService.getById(id);
        if (dish == null) {
            return R.error("错误信息");
        }
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> flavorList = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(flavorList);
        return R.success(dishDto);
    }

    @Override
    @Transactional
    public R<String> updateWithFlavor(DishDto dishDto) {
        Long id = dishDto.getId();
        dishService.updateById(dishDto);
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, id);
        dishFlavorService.remove(lqw);
        List<DishFlavor> dishFlavorList = dishDto.getFlavors();
        for (DishFlavor dishFlavor : dishFlavorList) {
            dishFlavor.setDishId(id);
        }
        dishFlavorService.saveBatch(dishFlavorList);
        return R.success("修改成功");
    }

    @Override
    public R<String> SaleByIds(int status, List<Long> ids) {
        List<Dish> dishes = dishService.listByIds(ids);
        List<Dish> updateDishes=new ArrayList<>();
        for (Dish dish : dishes) {
            dish.setStatus(status);
            updateDishes.add(dish);
        }
        dishService.updateBatchById(updateDishes);
        return R.success(status == 0 ? "停售成功" : "启售成功");
    }

    @Override
    public R<String> deleteByIds(List<Long> ids) {
        dishService.removeByIds(ids);
        return R.success("删除成功");
    }

    @Override
    public R<List<DishDto>> listDish(DishDto dishDto) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        //查找传输进来的菜品分类对应的菜品
        wrapper.eq(Dish::getCategoryId,dishDto.getCategoryId());
        //查找起售中的菜品
        wrapper.eq(Dish::getStatus,1);
        List<Dish> list = dishService.list(wrapper);
        //为每个菜品设置其对应的口味
        List<DishDto> dtoList = new ArrayList<>();
        for (Dish dish : list) {
            DishDto dto = new DishDto();
            BeanUtils.copyProperties(dish,dto);
            //根据菜品id获取其对应的口味
            Long id = dish.getId();
            LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
            lqw.eq(DishFlavor::getDishId,id);
            List<DishFlavor> dishFlavors = dishFlavorService.list(lqw);
            //设置口味
            dto.setFlavors(dishFlavors);
            dtoList.add(dto);
        }
        return R.success(dtoList);

    }
}
